import { Controller } from 'egg';
import { Op } from 'sequelize';
import { v4 as uuidv4 } from 'uuid';
import { yuanToFen } from '../../utils'

export default class GoodsController extends Controller {


  // 商品列表
  async list() {
    const { page, pageSize, ...where } = this.ctx.validateReq(
      'shop.list',
    ).query;

    const result = await this.ctx.service.shop.merchant.index(page, pageSize, where);

    if (result) {
      this.ctx.success({
        data: result
      });
    }
  }


  // 新建
  async create() {
    const { body } = this.ctx.validateReq('shop.create');

    Object.assign(body, { uuid: uuidv4() });
    const result = await this.ctx.service.shop.merchant.create(body);

    if (result) {
      this.ctx.success({
        data: {
          id: result.id,
        },
        status: 201,
      });
    }
  }


  // 编辑
  async update() {

    const { body, params: { id } } = this.ctx.validateReq('shop.update');

    const isExist = await this.ctx.service.shop.merchant.model.findOne({
      where: {
        id: { [Op.ne]: id },
        name: body.name,
      }
    });

    if (isExist) {
      return this.ctx.failure({
        code: 30020,
        data: {
          name: body.name,
        },
      });
    }

    const result = await this.ctx.service.shop.merchant.update(id, body);

    if (result) {
      this.ctx.success({
        data: {
          id: result.id,
        }
      });
    }
  }


  // 删除
  async destroy() {
    const { params: { id } } = this.ctx.validateReq('shop.destroy');

    const result = await this.ctx.service.shop.merchant.destroy(id);

    if (result) {
      this.ctx.success({
        data: {
          id: result.id,
        }
      });
    }
  }


  // 商铺配置
  async shopConfig() {
    const { body } = this.ctx.validateReq('shop.config');
    const id = this.ctx.state.user.shop_id;


    const toData = yuanToFen(body, ['pack', 'tableware']);
    const result = await this.ctx.service.shop.merchant.update(id, toData);

    if (result) {
      this.ctx.success({
        data: {
          id: result.id,
        }
      });
    }
  }

  // 商铺配置详情
  async shopConfigDetail() {
    const id = this.ctx.state.user.shop_id;
    const result = await this.ctx.service.shop.merchant.detail(id);

    if (result) {
      this.ctx.success({
        data: result
      });
    }
  }

  //  小程序商铺详情
  async shopDetail() {
    const id = this.ctx._shop.id;
    const result = await this.ctx.service.shop.merchant.detail(id);

    if (result) {
      this.ctx.success({
        data: result
      });
    }
  }

  // 查询商铺所属员工
  async users() {
    const query = this.ctx.validateReq('shop.users').params;

    const addArr = await this.ctx.model.AuthUserShop.findAll({
      where: {
        shop_id: query.id
      }
    });

    if (!addArr) {
      this.ctx.failure({ code: 10007 });
      return;
    }

    const allResult = await this.ctx.model.AuthUser.findAll({
      where: {
        type: 1
      }
    });

    const allArr = allResult.map(obj => ({
      key: obj.id,
      title: obj.username,
    }));

    this.ctx.success({
      data: {
        addList: addArr.map(it => it.user_id) || [],
        allList: allArr,
      },
    });
  }


  async usersUpdate() {
    const { params, body } = this.ctx.validateReq('shop.users.update');
    const shop_id = params.id;
    const idList = body.idList;

    await this.ctx.model.AuthUserShop.destroy({
      where: {
        shop_id
      }
    });

    const bulkParams: any[] = [];
    idList.forEach(user_id => bulkParams.push({
      user_id,
      shop_id
    }));
    const result = await this.ctx.model.AuthUserShop.bulkCreate(bulkParams);

    if (result === null) {
      this.ctx.failure({
        code: 50007,
        data: {
          idList,
        },
      });

      return false;
    }

    this.ctx.success();
  }

}
